<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Support\Facades\DB;

class BannerService
{
    /**
     * 增加首页banner图
     * @param $param
     * @return false|string
     */
    public static function addBanner($param){
        $param['remark'] = "增加首页banner图";
        $param['details'] = ToolUtil::handleSuccessOrError($param);
        if(DB::table('t_banner')->count('id')){
            return ToolUtil::returnJson(301,'已经存在首页banner图');
        }
        $data = [
            'banner_name'=>$param['banner_name'],
            'banner_img'=>$param['banner_img'],
            'vertical_img'=>$param['vertical_img'],
            'admin_id'=>$param['userId'],
            'create_time'=>time()
        ];
        if(DB::table('t_banner')->insert($data)){
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取首页banner图
     * @param $param
     * @return false|string
     */
    public static function queryBanner($param){
        $count = DB::table('t_banner')->count('id');
        $page = ToolUtil::calculationPage($param,$count);
        $data = DB::table('t_banner as b')
            ->leftJoin('t_admin as a','b.admin_id','=','a.id')
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get(['b.id','a.username','b.banner_name','b.banner_img','b.vertical_img','b.create_time','b.update_time']);
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data,$count);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取首页banner图
     * @param $param
     * @return false|string
     */
    public static function queryIdBanner($param){
        $data = DB::table('t_banner')->where('id',$param['id'])->first(['id','banner_name','banner_img','vertical_img']);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 修改首页banner图
     * @param $param
     * @return false|string
     */
    public static function editBanner($param){
        $param['remark'] = "修改首页banner图";
        $param['details'] = ToolUtil::handleSuccessOrError($param);
        $data = [
            'banner_name'=>$param['banner_name'],
            'banner_img'=>$param['banner_img'],
            'vertical_img'=>$param['vertical_img'],
            'update_time'=>time()
        ];
        if(DB::table('t_banner')->where('id',$param['id'])->update($data)){
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除首页banner图
     * @param $param
     * @return false|string
     */
    public static function deleteBanner($param){
        $param['remark'] = "删除首页banner图";
        $banner = DB::table('t_banner')->where('id',$param['id'])->first(['id','banner_name','banner_img']);
        $param['details'] = ToolUtil::handleSuccessOrError($banner);
        if(DB::table('t_banner')->where('id',$param['id'])->delete()){
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }
}